package com.ttai.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.squareup.picasso.Picasso;
import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerAcountItemComponet;
import com.ttai.dagger.module.activity.AcountItemPresenterModule;
import com.ttai.presenter.activity.AcountItemPresenter;
import com.ttai.ui.adapter.LoginIdRecycleViewAdapter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.other.DecryptDialog;
import com.ttai.untils.Constant;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AcountItem extends BaseActivity {

    @Inject
    AcountItemPresenter acountItemPresenter;

    @Bind(R.id.pb_bar)
    ProgressBar pbBar;
    @Bind(R.id.tv_passwordtip)
    EditText tvPasswordtip;
    @Bind(R.id.et_passwordkey)
    EditText etPasswordkey;
    @Bind(R.id.tb_key_visibility)
    ToggleButton tbKeyVisibility;
    @Bind(R.id.btn_jiemi)
    Button btnJiemi;

    DecryptDialog decryptDialog;

    private String message = "";
    private String aname;
    private String aphone;
    public String secretKey;
    private String value;
    private String imgUrl;
    /**
     * 设置计时器
     */
    private CountDownTimer timer = new CountDownTimer(30000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            decryptDialog.getTvMessgae().setText("本界面将在"+(millisUntilFinished / 1000)+"秒后自动关闭,请尽快登录");
        }

        @Override
        public void onFinish() {
            finish();
        }
    };
    private CountDownTimer bartime = new CountDownTimer(2000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
        }

        @Override
        public void onFinish() {
            startpresenter();
            pbBar.setVisibility(View.INVISIBLE);
        }
    };


    private void startpresenter() {
        /**
         * 隐藏软键盘
         */
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);

        String token = Constant.getToken();
        System.out.println("token:" + token + "aname" + aname);
        acountItemPresenter.deCode(token, aname);
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acountcontext);
        ButterKnife.bind(this);

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("账号密码解密");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        setComponet();


        tbKeyVisibility.setOnCheckedChangeListener(new ToggleButtonClick());

//        acountItemPresenter.dealDB();
        Intent intent = getIntent();
        aname = intent.getStringExtra("aname");
        aphone = intent.getStringExtra("aphone");
        value = intent.getStringExtra("value");
        imgUrl = intent.getStringExtra("img");

        Log.i("555", "onCreate: tip:" + value);
        if (value != null && value.isEmpty() == false) {
            tvPasswordtip.setText(value);
            tvPasswordtip.setInputType(InputType.TYPE_NULL);
        }



    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void onStop() {

        super.onStop();
        this.finish();
    }

    @Override
    public void setComponet() {
        DaggerAcountItemComponet.builder()
                .acountItemPresenterModule(new AcountItemPresenterModule(this))
                .build()
                .in(this);
    }

    public void setText() {
        timer.start();
        decryptDialog = new DecryptDialog(this);
        decryptDialog.setAccountname(aname+": ");
        decryptDialog.setAccountnumber(acountItemPresenter.getXorAcount());
        decryptDialog.setPassword("密码 : "+acountItemPresenter.getXorPassword());
        System.out.println("img  :"+imgUrl);
        decryptDialog.setImgUrl(imgUrl);
//        Picasso.with(this).load(imgUrl).placeholder(R.drawable.xike1).into(decryptDialog.getIvicon());
        decryptDialog.show();
    }

    @OnClick(R.id.btn_jiemi)
    public void onViewClicked() {
        secretKey = etPasswordkey.getText().toString();
        if (secretKey == null || secretKey.isEmpty()) {
            Toast.makeText(AcountItem.this, "秘钥不能为空", Toast.LENGTH_SHORT).show();
        } else {
            pbBar.setVisibility(View.VISIBLE);

            bartime.start();
//            /**
//             * 隐藏软键盘
//             */
//            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
//
//            String token = Constant.getToken();
//            System.out.println("token:" + token + "aname" + aname);
//            acountItemPresenter.deCode(token, aname);
//                    启动计时器
        }
    }

    private class ToggleButtonClick implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //5、判断事件源的选中状态
            if (isChecked) {

                //显示密码
                //etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etPasswordkey.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            } else {
                // 隐藏密码
                //etPassword.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                etPasswordkey.setTransformationMethod(PasswordTransformationMethod.getInstance());

            }
            //6、每次显示或者关闭时，密码显示编辑的线不统一在最后，下面是为了统一
            etPasswordkey.setSelection(etPasswordkey.length());
        }
    }
}
